package com.jchy.renderit.core;
 
import java.util.Vector;

public class MultiPrimitiveObject extends Displayable
{
    public Vector<Displayable> multi;
    
    public MultiPrimitiveObject()
    {
        multi=new Vector<Displayable>();
    }
    
    public void addToScene(Vector<Displayable> inVector)
    {
        for(int i=0;i<multi.size();i++)
        {
            inVector.addElement(multi.elementAt(i));
        }
    }
    public void setPrimaryColorInt(int inred, int ingreen, int inblue)
    {
        super.setPrimaryColorInt(inred,ingreen,inblue);
        for(int i=0;i<multi.size();i++)
        {
            ((Displayable)multi.elementAt(i)).setPrimaryColorInt(inred,ingreen,inblue);
        }
    }

    public void SetShading(double inAmbient, double inDiffuse, double inPhongCoef, double inPhongExp)
    {
        super.SetShading(inAmbient,inDiffuse,inPhongCoef,inPhongExp);
        for(int i=0;i<multi.size();i++)
        {
            ((Displayable)multi.elementAt(i)).SetShading(inAmbient,inDiffuse,inPhongCoef,inPhongExp);
        }
    }

    public Hit intersect(Point prp, Point vrp)
    {
        Hit   aHit = new Hit();        
        Point utprp, utvrp;

        utprp = untransform(prp);
        utvrp = untransform(vrp);

        Hit minHit=new Hit();
        minHit.isHit=false;
        double aDist;
        double minDist=Double.MAX_VALUE;

        for(int i=0;i<multi.size();i++)
        {
            aHit=((Displayable)multi.elementAt(i)).intersect(utprp,utvrp);
            if(aHit.isHit)    
            {
                aDist=aHit.location.Dist(utprp);
                if(aDist<minDist)
                {
                    minDist=aDist;
                    minHit=aHit.copy();
                }   
            }
        }
        minHit.location=transform(minHit.location);
        minHit.normpt=transform(minHit.normpt);

        return minHit;
    }


    public String toString()
    {
        String result=super.toString();
        result=result + "\nNumber of Objects: " + multi.size();
        return result;
    }
}

